using System;
using UnityEngine;

[ExecuteInEditMode]
public class PresetsPipeCorner : MonoBehaviour
{
	public float angle;

	public bool lockLenght = true;

	public bool lockHeight;

	private ControllerPipeCorner controller;

	private bool lockLenghtOld;

	private bool lockHeightOld = true;

	private float lockedHeight;

	private float lockedLenght;

	private bool init = true;

	private void LateUpdate()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		if (controller == null)
		{
			controller = base.gameObject.GetComponent<RemoteControl>().controller.GetComponent<ControllerPipeCorner>();
		}
		if (init)
		{
			angle = controller.unghiRampa;
			lockLenghtOld = lockLenght;
			lockHeightOld = lockHeight;
			if (lockLenght)
			{
				lockedLenght = base.transform.localScale.z;
			}
			if (lockHeight)
			{
				lockedHeight = base.transform.localScale.y;
			}
			init = false;
		}
		if (lockLenghtOld != lockLenght)
		{
			if (lockLenght)
			{
				lockedLenght = base.transform.localScale.z;
			}
			lockLenghtOld = lockLenght;
		}
		if (lockHeightOld != lockHeight)
		{
			if (lockHeight)
			{
				lockedHeight = base.transform.localScale.y;
			}
			lockHeightOld = lockHeight;
		}
		if (!lockHeight && !lockLenght)
		{
			angle = GetAngle();
		}
		else if (lockHeight && lockLenght)
		{
			base.transform.localScale = new Vector3(base.transform.localScale.x, lockedHeight, lockedLenght);
			angle = GetAngle();
		}
		else if (lockHeight)
		{
			angle = Mathf.Clamp(angle, 1f, 89f);
			base.transform.localScale = new Vector3(base.transform.localScale.x, lockedHeight, base.transform.localScale.z);
			SeteazaUnghiPastrezaInaltimea(angle);
		}
		else if (lockLenght)
		{
			angle = Mathf.Clamp(angle, 1f, 89f);
			base.transform.localScale = new Vector3(base.transform.localScale.x, base.transform.localScale.y, lockedLenght);
			SeteazaUnghiPastreazaLungimea(angle);
		}
		controller.unghiRampa = angle;
	}

	public float GetAngle()
	{
		float x = base.transform.localScale.x;
		float z = base.transform.localScale.z;
		float y = base.transform.localScale.y;
		float num = Mathf.Sqrt((z - x) * (z - x) + (y - x) * (y - x));
		return Mathf.Acos((z - x) / num) * 57.29578f;
	}

	public void SeteazaUnghiPastrezaInaltimea(float unghi)
	{
		float x = base.transform.localScale.x;
		float y = base.transform.localScale.y;
		float z = x + (y - x) / Mathf.Tan(unghi * ((float)Math.PI / 180f));
		base.transform.localScale = new Vector3(x, y, z);
	}

	public void SeteazaUnghiPastreazaLungimea(float unghi)
	{
		float x = base.transform.localScale.x;
		float z = base.transform.localScale.z;
		float y = x + (z - x) * Mathf.Tan(unghi * ((float)Math.PI / 180f));
		base.transform.localScale = new Vector3(x, y, z);
	}
}
